package com.djinfo.work.controller;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.entity.DicVo;
import com.djinfo.work.entity.Dictionaries;
import com.djinfo.work.service.DictionariesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/5/19.
 */
/**
 * 部门
 */
@Controller
@RequestMapping("/dictionaries")
public class DictionariesController {

    /**
     * 自动注入
     */
    @Autowired
    private DictionariesService dictionariesService;


    /**
     * 部门列表
     */
    @RequestMapping("/list.do")
    @ResponseBody
    public AjaxJson list(Dictionaries dictionaries, Integer pagenum, Integer pagesize){

        AjaxJson ajaxJson = dictionariesService.list(dictionaries,pagenum,pagesize);
        return ajaxJson;
    }


    /**
     * 获取部门对象信息
     */
    @RequestMapping("/info.do")
    public ModelAndView info(Dictionaries dictionaries, Model model){
        HashMap<String, Object> map = new HashMap<>();
        if(dictionaries.getId()==null){
            dictionaries=new Dictionaries();
        }else {
            dictionaries = dictionariesService.getDepartmentById(dictionaries.getId());
        }
        return new ModelAndView("dictionaries/edit.html","dictionaries",dictionaries);
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate.do")
    @ResponseBody
    public AjaxJson saveOrUpdate(Dictionaries dictionaries,HttpSession session){
        AjaxJson ajaxJson = dictionariesService.saveOrUpdate(dictionaries,session);
        return ajaxJson;
    }
    /**
     * 删除
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public AjaxJson delete(Dictionaries dictionaries){
        AjaxJson ajaxJson = dictionariesService.deleteDictionary(dictionaries);
        return ajaxJson;
    }

   /* @RequestMapping("/allDictionaries.do")
    @ResponseBody
    public AjaxJson deptList(Dictionaries dictionaries){
        AjaxJson ajaxJson = new AjaxJson();
        List list=dictionariesService.list();
        ajaxJson.setCode(101);
        HashMap<String, Object> map =new HashMap();
        map.put("list",list);
        ajaxJson.setResult(map);
        return ajaxJson;
    }*/

   /* *//**
     * 批量删除
     *//*
    @RequestMapping("/deleteSome.do")
    @ResponseBody
    public AjaxJson deleteSome(String[] ids){
        AjaxJson ajaxJson = new AjaxJson();
        for(int i=0;i<ids.length;i++){
            Dictionaries dictionaries=dictionariesService.getDepartmentById(ids[i]);
            dictionariesService.delete(dictionaries);
        }
        ajaxJson.setCode(101);
        ajaxJson.setMsg("success");
        return ajaxJson;
    }*/
    /**
     * 批量删除
     */
    @RequestMapping("/getDic.do")
    @ResponseBody
    public List<DicVo> getDic(String type){
        AjaxJson ajaxJson = new AjaxJson();
        List<DicVo> dicList = dictionariesService.getDic(type);



        return dicList;
    }
}
